package com.jiyun.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.jiyun.login.dto.UpdatePasswordDto;
import com.jiyun.login.dto.UserInfoDto;
import com.jiyun.login.pojo.User;
import com.jiyun.login.vo.CosUploadVo;
import com.jiyun.login.vo.UserInfoVo;
import com.jiyun.result.Result;
import com.jiyun.service.UsersService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author zqtao
 */

@Slf4j
@Tag(name = "用户API接口管理")
@RestController
@RequestMapping("/users")
public class UsersController {

    @Autowired
    private UsersService usersService;

    @Operation(summary = "登录方法")
    @PostMapping("/login")
    public Result<String> login(@RequestBody User user) {
        return Result.ok(usersService.login(user));
    }

    @Operation(summary = "获取登录人信息")
    @GetMapping("/info")
    public Result<User> info() {
        Long loginId = Long.parseLong(StpUtil.getLoginId().toString());
        return Result.ok(usersService.getInfo(loginId));
    }

    @Operation(summary = "图片上传方法")
    @PostMapping("/upload")
    public Result<CosUploadVo> upload(@RequestPart("file") MultipartFile file, @RequestParam(name = "path", defaultValue = "auth") String path) {
        return Result.ok(usersService.upload(file, path));
    }

    @Operation(summary = "保存用户信息方法")
    @PostMapping("/update")
    public Result<String> update(@RequestBody UserInfoDto userInfoDto) {
        Long loginId = Long.parseLong(StpUtil.getLoginId().toString());
        return Result.ok(usersService.updateInfo(userInfoDto, loginId));
    }

    @Operation(summary = "获取图片和昵称")
    @GetMapping("/findNickAvatar")
    public Result<UserInfoVo> findNickAvatar() {
        Long loginId = Long.parseLong(StpUtil.getLoginId().toString());
        return Result.ok(usersService.findNickAvatar(loginId));
    }

    @Operation(summary = "修改密码")
    @PostMapping("/updatePassword")
    public Result<String> updatePassword(@RequestBody UpdatePasswordDto updatePasswordDto) {
        Long loginId = Long.parseLong(StpUtil.getLoginId().toString());
        return Result.ok(usersService.updatePassword(updatePasswordDto, loginId));
    }

    @Operation(summary = "退出登录")
    @GetMapping("/logout")
    public Result<String> logout() {
        StpUtil.logout();
        return Result.ok("退出成功");
    }


}
